﻿using PayexpDAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace Moneymatters
{
    public partial class ScanAndPrint : Form
    {
        private string _selectedDocType;

        public string SelectedDocType
        {
            get { return _selectedDocType; }
        }

        private string _selectedFilePath;

        public string SelectedFilePath
        {
            get { return _selectedFilePath; }
        }

        private string folderPathToSaveUpdateDocument = string.Empty;

        private List<string> docTypeList = new List<string>();

        public ScanAndPrint(string currentDocumentPath)
        {
            InitializeComponent();

            //this.folderPathToSaveUpdateDocument = AppSettingsLoader.AppSettingsValuesMap[Constants.AppSettingsKeys.ScannedImageLocation];
            this.folderPathToSaveUpdateDocument = SettingsAndStaticListLoader.GetAppSettingValue(Constants.AppSettingsKeys.ScannedImageLocation);

            this.linkLabelExistingDocument.Click += linkLabelExistingDocument_Click;
            if (!string.IsNullOrEmpty(currentDocumentPath))
            {
                this.linkLabelExistingDocument.Text = "Click to view existing document";
                this.linkLabelExistingDocument.Tag = currentDocumentPath;
            }
            else
            {
                this.linkLabelExistingDocument.Text = "No document exists";
                this.linkLabelExistingDocument.Tag = null;
            }

            var docTypeStaticList = SettingsAndStaticListLoader.StaticListMap[Constants.TableConstants.StaticListConstants.doctype];
            docTypeList.AddRange(docTypeStaticList.Select(t => t.ListItemValue));

            this.comboBoxDocumenType.SelectedIndexChanged += comboBoxDocumenType_SelectedIndexChanged;
            this.comboBoxDocumenType.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.comboBoxDocumenType.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            
            docTypeList.ForEach(t => this.comboBoxDocumenType.Items.Add(t));
            this.comboBoxDocumenType.SelectedItem = "Cheque";
        }

        void linkLabelExistingDocument_Click(object sender, EventArgs e)
        {
            if (this.linkLabelExistingDocument.Text.Equals("No document exists"))
            {
                MessageBox.Show("You have not uploaded any document.", "No document exists", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                if(this.linkLabelExistingDocument.Tag != null)
                {
                    string filePath = (string)this.linkLabelExistingDocument.Tag;
                    try
                    {
                        Process.Start(filePath);
                    }
                    catch 
                    {
                        MessageBox.Show("There was an error displaying your uploaded document.", "Error displaying document", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    };
                }
            }
        }

        void comboBoxDocumenType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._selectedDocType = this.comboBoxDocumenType.SelectedItem.ToString();
        }

        private void buttonOpenFileDialog_Click(object sender, EventArgs e)
        {
            OpenFilePickerDialog();
            DialogResult dr = System.Windows.Forms.DialogResult.Yes;
            if (string.IsNullOrEmpty(this._selectedFilePath))
            {
                while (dr == System.Windows.Forms.DialogResult.Yes)
                {
                    dr =
                        MessageBox.Show("You have not selected any document. Do you wish to select again?", "Document not selected", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        break;
                    }
                    else if (dr == System.Windows.Forms.DialogResult.Yes)
                    {
                        OpenFilePickerDialog();
                        if (!string.IsNullOrEmpty(this._selectedFilePath))
                        {
                            break;
                        }
                    }
                    else
                    {
                        dr = System.Windows.Forms.DialogResult.Yes;
                    }
                }
            }
        }

        private void OpenFilePickerDialog()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            this._selectedFilePath = openFileDialog.FileName;
            this.labelSelectedPath.Text = this._selectedFilePath;
            this.labelSelectedPath.Refresh();
        }
    }
}
